package com.crm.business.crm.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@EnableConfigurationProperties(ThreadPoolConfigurationProperties.class)
@Configuration //声明配置类
@EnableAsync //开启异步任务支持
public class TaskExecutorConfig implements AsyncConfigurer {

    @Autowired
    ThreadPoolConfigurationProperties properties;

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(properties.getCoreSize());//线程池大小
        taskExecutor.setMaxPoolSize(properties.getMaxSize());//线程池最大线程数
        taskExecutor.setQueueCapacity(properties.getQueueCapacity());//最大等待任务数
        taskExecutor.setKeepAliveSeconds(properties.getKeepAliveTime());
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return AsyncConfigurer.super.getAsyncUncaughtExceptionHandler();
    }
}
